package com.midou.administration.utils;

import com.alibaba.fastjson.JSONObject;
import com.zhenzi.sms.ZhenziSmsClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/*发送短信类*/
@Component
public class Information {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 发送验证码的方法
     * @param phone 手机号
     * @param code  验证码
     * @return  是否发送成功
     */
    public boolean send(String phone,String code) {
        String result = null;
        ZhenziSmsClient client = new ZhenziSmsClient(DxConfig.apiUrl, DxConfig.appId, DxConfig.appSecret);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("number", phone);
        params.put("templateId", DxConfig.templateId );
        String[] templateParams = new String[1];
        templateParams[0] = code;
        params.put("templateParams", templateParams);
        try {
            result = client.send(params);
            System.out.println(result);

        } catch (Exception e) {
            e.printStackTrace();
        }
        if(!analysis(result)){
//           返回是否成功
            return false;
        }
//        发送成功
        System.out.println("发送成功");
        return true;
    }

    /**
     * 生成4位数验证码
     * @return 验证码
     */
    public String generateCode(){
        String verifyCode = String.valueOf(new Random().nextInt(8999) + 1000);
        System.out.print("验证码: "+verifyCode);
        return verifyCode;
    }

    /**
     * 验证验码是否有效
     * @param phone   验证的手机号
     * @param code    输入的验证码
     * @return  是否有效
     */
    public int verify(String phone,String code){
        System.out.println("传递的编码是:"+code);
        try {
            boolean hasKey = stringRedisTemplate.hasKey(phone);
            if(hasKey){
                //存在
                String result = this.stringRedisTemplate.opsForValue().get(phone);
                System.out.println("获取到的result是:"+result);
                if(code.equals(result)){
                    //正确
                    return 0;
                }else{
                    //不正确
                    return 1;
                }
            }else{
                //不存在
                return 2;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return 2;

    }

    /**
     * 将验证码保存在redis当中
     * @param phone
     * @param code
     */
    public void store(String phone,String code){
        System.out.println("Phone:" + phone);
        System.out.println("Code:" + code);
        this.stringRedisTemplate.opsForValue().set(phone,code);
        this.stringRedisTemplate.expire(phone,180, TimeUnit.SECONDS );
    }

    public Boolean analysis(String result){
       JSONObject json = JSONObject.parseObject(result);
       if((json.getIntValue("code") != 0)){
//           发送短信失败
            return false;
        }
        return true;
    }



}
